package com.ruoyi.framework.web.service;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqUserVisit;
import com.ruoyi.wq.service.IWqUserVisitService;
import com.ruoyi.wx.domain.WxMaMsgForm;
import com.ruoyi.wx.domain.WxMpMsgForm;
import com.ruoyi.wx.service.WxMsgService;
import cn.hutool.core.date.DateUtil;

@Component
public class UserVisitService {
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
	private IWqUserVisitService wqUserVisitService;
	
	@Autowired
    private WxMsgService wxMsgService;

	public int addVisit(Long userId) {
		Long visitUserId = SecurityUtils.getUserId();
		SysUser user = sysUserService.getById(userId);
		SysUser sysUser = sysUserService.getById(visitUserId);
		
		QueryWrapper<WqUserVisit> queryWrapper = new QueryWrapper<>();
		WqUserVisit entity = new WqUserVisit();
		entity.setVisitUserId(visitUserId);
		entity.setUserId(userId);
		queryWrapper.setEntity(entity);
		WqUserVisit visit = wqUserVisitService.getOne(queryWrapper);
		if(visit == null){
			WxMpMsgForm mpForm = new WxMpMsgForm();
			mpForm.setWxId(user.getWxId());
			mpForm.setAppId(user.getAppId());
			mpForm.setOpenId(user.getWxOpenId());
    		Map<String,String> mpMap = new HashMap<>();
    		mpMap.put("first", "您好，您收到一条访客通知");
    		mpMap.put("keyword1", sysUser.getNickName());
    		mpMap.put("keyword2", DateUtil.now());
    		mpMap.put("keyword3", "您的主页有一个新访客");
    		mpMap.put("remark", "本条信息是站内信通知，祝您早日脱单！点击查看详情");
    		mpForm.setDataMap(mpMap);
    		mpForm.setPage("pages/index");
    		boolean flag = wxMsgService.sendTemplateMsg(mpForm);
			if(!flag){
				WxMaMsgForm msgForm = new WxMaMsgForm();
				msgForm.setAppId(user.getAppId());
				msgForm.setOpenId(user.getOpenId());
				Map<String,String> dataMap = new HashMap<>();
				dataMap.put("phrase1", "访客通知");
				dataMap.put("date2", DateUtil.now());
				dataMap.put("name3", sysUser.getNickName());
				dataMap.put("thing4", "您好，您收到一条访客通知");
				msgForm.setDataMap(dataMap);
				msgForm.setPage("pages/index");
				wxMsgService.sendSubscribeMsg(msgForm);
			}
        		
			visit = new WqUserVisit();
			visit.setVisitUserId(sysUser.getUserId());
			visit.setVisitSex(sysUser.getSex());
			visit.setVisitAvatar(sysUser.getAvatar());
			visit.setVisitNickName(sysUser.getNickName());
			visit.setVisitAge(sysUser.getAge());
			visit.setCity(sysUser.getCity());
			visit.setLon(sysUser.getLon());
			visit.setLat(sysUser.getLat());
			visit.setUserId(userId);
			wqUserVisitService.save(visit);
		}else{
			Integer visitNum = visit.getVisitNum();
			visit.setVisitSex(sysUser.getSex());
			visit.setVisitAvatar(sysUser.getAvatar());
			visit.setVisitNickName(sysUser.getNickName());
			visit.setVisitAge(sysUser.getAge());
			visit.setCity(sysUser.getCity());
			visit.setLon(sysUser.getLon());
			visit.setLat(sysUser.getLat());
			visit.setVisitNum(visitNum+1);
			wqUserVisitService.updateById(visit);
		}
		
		
		QueryWrapper<WqUserVisit> qw = new QueryWrapper<>();
		WqUserVisit uv = new WqUserVisit();
		uv.setUserId(userId);
		qw.setEntity(uv);
		return wqUserVisitService.count(qw);
	}

	public Integer addVisitUser(Long userId, Long visitUserId) {
		if(visitUserId == null){
			return null;
		}
		SysUser user = sysUserService.getById(userId);
		SysUser sysUser = sysUserService.getById(visitUserId);
		
		QueryWrapper<WqUserVisit> queryWrapper = new QueryWrapper<>();
		WqUserVisit entity = new WqUserVisit();
		entity.setVisitUserId(visitUserId);
		entity.setUserId(userId);
		queryWrapper.setEntity(entity);
		WqUserVisit visit = wqUserVisitService.getOne(queryWrapper);
		if(visit == null){
			WxMpMsgForm mpForm = new WxMpMsgForm();
			mpForm.setWxId(user.getWxId());
			mpForm.setAppId(user.getAppId());
			mpForm.setOpenId(user.getWxOpenId());
    		Map<String,String> mpMap = new HashMap<>();
    		mpMap.put("first", "您好，您收到一条访客通知");
    		mpMap.put("keyword1", sysUser.getNickName());
    		mpMap.put("keyword2", DateUtil.now());
    		mpMap.put("keyword3", "您的主页有一个新访客");
    		mpMap.put("remark", "本条信息是站内信通知，祝您早日脱单！点击查看详情");
    		mpForm.setDataMap(mpMap);
    		mpForm.setPage("pages/index");
    		boolean flag = wxMsgService.sendTemplateMsg(mpForm);
			if(!flag){
				WxMaMsgForm msgForm = new WxMaMsgForm();
				msgForm.setAppId(user.getAppId());
				msgForm.setOpenId(user.getOpenId());
				Map<String,String> dataMap = new HashMap<>();
				dataMap.put("phrase1", "访客通知");
				dataMap.put("date2", DateUtil.now());
				dataMap.put("name3", sysUser.getNickName());
				dataMap.put("thing4", "您好，您收到一条访客通知");
				msgForm.setDataMap(dataMap);
				msgForm.setPage("pages/index");
				wxMsgService.sendSubscribeMsg(msgForm);
			}
        		
			visit = new WqUserVisit();
			visit.setVisitUserId(sysUser.getUserId());
			visit.setVisitSex(sysUser.getSex());
			visit.setVisitAvatar(sysUser.getAvatar());
			visit.setVisitNickName(sysUser.getNickName());
			visit.setVisitAge(sysUser.getAge());
			visit.setCity(sysUser.getCity());
			visit.setLon(sysUser.getLon());
			visit.setLat(sysUser.getLat());
			visit.setUserId(userId);
			wqUserVisitService.save(visit);
		}else{
			Integer visitNum = visit.getVisitNum();
			visit.setVisitSex(sysUser.getSex());
			visit.setVisitAvatar(sysUser.getAvatar());
			visit.setVisitNickName(sysUser.getNickName());
			visit.setVisitAge(sysUser.getAge());
			visit.setCity(sysUser.getCity());
			visit.setLon(sysUser.getLon());
			visit.setLat(sysUser.getLat());
			visit.setVisitNum(visitNum+1);
			wqUserVisitService.updateById(visit);
		}
		
		
		QueryWrapper<WqUserVisit> qw = new QueryWrapper<>();
		WqUserVisit uv = new WqUserVisit();
		uv.setUserId(userId);
		qw.setEntity(uv);
		return wqUserVisitService.count(qw);
	}
	
}
